/*
 * Copyright @ 苏州瑞云信息技术有限公司 All rights reserved.
 */
package work.base.data.list;


import work.entity.Student;
import work.entity.User;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 */
public class ArrayListTest02 {

    public static void main(String[] args) {
        ArrayListTest02 arrayListTest02 = new ArrayListTest02();
        arrayListTest02.listContains();
    }

    /**
     * list的contains() 和indexOf() 最终调用的equals方法进行比较
     *
     */
    public void listContains() {
        User user1 = new User("1", "A", "A");
        User user2 = new User("2", "B", "B");
        User user3 = new User("3", "A", "A");
        List<User> list1 = new ArrayList<>();
        list1.add(user1);
        list1.add(user2);
        Integer index = list1.indexOf(user2);
        System.out.println(list1.contains(new User("2", "B", "B")));
        System.out.println(list1.contains(user2));
        System.out.println(list1.contains(user3));
    }


    /**
     * Lambda表达式内部关于变量的使用
     *
     * 注意
     *      1 forEach函数 外部变量如果不使用final的话 可以使用Atomic包
     *      2 forEach函数(lambda表达式) 需要外部变量不可变的（只是引用不可变，而不是真正的不可变）
     */
    public void listForEach() {
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(new Student("aa", "111"));
        studentList.add(new Student("bb", "222"));
        AtomicInteger countNum = new AtomicInteger(100);
        Student student = new Student("cc", "333");
        studentList.forEach(stu -> {
            countNum.getAndIncrement();
            student.setName("沈明龙");
        });
        System.out.println(countNum);
        System.out.println(student);
    }

}
